/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.todoterreno.beans;

import com.todoterreno.dao.GestionImagenesInter;
import com.todoterreno.dao.impl.GestionImagenesInterDao;
import com.todoterreno.model.CasaTerreno;
import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.util.List;
import javax.faces.context.FacesContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author davex
 */
public class HomeBean implements Serializable {

    private String root = "/home/davidpanel/portada/";
    private List<String> images;
    private StreamedContent streamImagen;
    private GestionImagenesInter obtImagen = new GestionImagenesInterDao();

    public HomeBean() {
        try {

            images = obtImagen.consultarImagenesPortada();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public List<String> getImages() {
        return images;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    public StreamedContent getStreamImagen() {
        try {

            FacesContext context = FacesContext.getCurrentInstance();
            if (context.getRenderResponse()) {
                return new DefaultStreamedContent();
            } else {
                String name = context.getExternalContext().getRequestParameterMap().get("nameImage");
                File imgFile = new File(root + name);
                StreamedContent ds = new DefaultStreamedContent(new FileInputStream(imgFile), "image/jpg");
                return ds;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public void setStreamImagen(StreamedContent streamImagen) {
        this.streamImagen = streamImagen;
    }

    public String getRoot() {
        return root;
    }

    public void setRoot(String root) {
        this.root = root;
    }
    
}
